package com.mall.controller;

import com.mall.domain.dto.CreateOrderDTO;
import com.mall.domain.dto.OrderPageQueryDTO;
import com.mall.domain.dto.UpdateAddressDTO;
import com.mall.domain.po.Order;
import com.mall.domain.vo.OrderDetailVO;
import com.mall.domain.vo.OrderVO;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 订单管理控制器
 * @author 勾新杰
 * @version 1.0
 * @date 2024/11/25 10:44
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
@Tag(name = "订单管理", description = "订单管理相关接口")
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单
     * @param createOrderDTO 创建订单传输对象
     * @return 创建订单结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建订单")
    public Result<String> createOrder(@RequestBody CreateOrderDTO createOrderDTO) {
        orderService.createOrder(createOrderDTO);
        return Result.success("创建订单成功");
    }

    /**
     * 修改订单地址
     * @param updateAddressD 修改订单地址传输对象
     * @return 修改订单地址结果
     */
    @PutMapping("/address")
    @Operation(summary = "修改订单地址")
    public Result<String> updateAddress(@RequestBody UpdateAddressDTO updateAddressD) {
        orderService.updateAddress(updateAddressD);
        return Result.success("修改订单地址成功");
    }

    /**
     * 订单详情
     * @param id 订单id
     * @return 订单详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "订单详情")
    public Result<OrderDetailVO> orderDetail(@PathVariable Long id) {
        OrderDetailVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 订单列表，管理端会用
     * @return 订单列表
     */
    @PostMapping("/page")
    @Operation(summary = "订单列表分页查询")
    public Result<PageResult<OrderVO>> page(@RequestBody OrderPageQueryDTO orderPageQueryDTO) {
        PageResult<OrderVO> pageResult = orderService.page(orderPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 发货
     * @param id 订单id
     * @return 发货结果
     */
    @PutMapping("/deliver/{id}")
    @Operation(summary = "发货")
    public Result<String> deliver(@PathVariable Long id) {
        orderService.deliver(id);
        return Result.success("发货成功");
    }

    /**
     * 批量发货
     * @param ids 订单id集合
     * @return 发货结果
     */
    @PutMapping("/deliver/batch")
    @Operation(summary = "批量发货")
    public Result<String> deliverBatch(@RequestBody List<Long> ids) {
        orderService.deliverBatch(ids);
        return Result.success("批量发货成功");
    }

    /**
     * 取消订单
     * @param id 订单id
     * @return 取消订单结果
     */
    @DeleteMapping("/cancel/{id}")
    @Operation(summary = "取消订单")
    public Result<String> cancelOrder(@PathVariable Long id) {
        orderService.cancelOrder(id);
        return Result.success("取消订单成功");
    }

    /**
     * 确认收货
     * @param id 订单id
     * @return 收货结果
     */
    @PutMapping("/receipt/{id}")
    @Operation(summary = "确认收货")
    public Result<String> confirmReceipt(@PathVariable Long id) {
        orderService.confirmReceipt(id);
        return Result.success("确认收货成功");
    }
}
